package com.gac.keepalive

import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager

/**
 * FileName: OptimizeBatteryHelper
 * Author: gacmy
 * Date: 2021/10/9 1:40 PM
 * Description:
 */
class OptimizeBatteryHelper(activity: FragmentActivity) {

     private val mActivity: FragmentActivity by lazy{
          activity
     }


     companion object{
          const val TAG_PERMISSION = "TAG_PERMISSION"

          fun init(fragmentActivity: FragmentActivity): OptimizeBatteryHelper {
               return OptimizeBatteryHelper(fragmentActivity)
          }
     }

     fun requestBattery(listener: IPermissionListener){
          getFragment(mActivity)?.requestBattery(listener)
     }

     private fun getFragment(activity: FragmentActivity): OptimizeBattery? {
          var fragment: OptimizeBattery? =
               mActivity.supportFragmentManager.findFragmentByTag(TAG_PERMISSION) as OptimizeBattery?
          if (fragment == null) {
               fragment = OptimizeBattery()
               val fragmentManager: FragmentManager = activity.supportFragmentManager
               fragmentManager
                    .beginTransaction()
                    .add(fragment, TAG_PERMISSION)
                    .commitAllowingStateLoss()
               fragmentManager.executePendingTransactions()
          }
          return fragment
     }

}